// Login Form
$(function() {
    var button = $('#loginButton');
    var box = $('#loginBox');
    var form = $('#loginForm');
    button.removeAttr('href');
    button.mouseup(function(login) {
        box.toggle();
        button.toggleClass('active');
    });
    form.mouseup(function() { 
        return false;
    });
    $(this).mouseup(function(login) {
        if(!($(login.target).parent('#loginButton').length > 0)) {
            button.removeClass('active');
            box.hide();
        }
    });

    
    
    $("#login").click(
      function(){
        var loginData = $("#loginForm").serialize();
        var ajaxURL = AJAX_URL + 'login/';
    
        // Ajax upit
        $.ajax({  
            type: "POST", 
            url: ajaxURL,
            data: loginData,
            cache: false,
            async: false,
            success: function(response){

                var parsed = JSON.parse(response);
                var html = '';

                if(parseInt(parsed.ind) == 1)
                {
                    // Nakon logovanja ide na stranicu profila
                    window.location = parsed.redirect_url;
                }
                else
                {      
                    html += "<p>"+parsed.msg +"</p>";
                    $('#response_login').show().html(html).delay(2500).hide(500); 
                }            
            }  
        }); // end AJAX upita 
    });
    
    $("#logout").click(
      function(){
        
        var ajaxURL = AJAX_URL + 'logout/';
    
        // Ajax upit
        $.ajax({  
            type: "POST", 
            url: ajaxURL,
            //data: loginData,
            cache: false,
            async: false,
            success: function(response){

                var parsed = JSON.parse(response);
                var html = '';

                if(parseInt(parsed.ind) == 1)
                {
                    window.location = BASE_URL;
                }
                else
                {      
                    html += "<p>"+parsed.msg +"</p>";
                    $('#response_login').show().html(html).delay(2500).hide(500); 
                }            
            }  
        }); // end AJAX upita 
        
    });
    
    
    $("#forgot-password").click(
      function(){
        
        var email = $("#email").val();
        
        var ajaxURL = AJAX_URL + 'forgot_password/';
    
        // Ajax upit
        $.ajax({  
            type: "POST", 
            url: ajaxURL,
            data: 'email='+email,
            cache: false,
            async: false,
            success: function(response){

                var parsed = JSON.parse(response);
                var html = '';
                html += "<p>"+parsed.msg +"</p>";
                $('#response').show().html(html).delay(2500).hide(500); 
                if(parseInt(parsed.ind) == 1)
                {                    
                    setInterval(function(){window.location = BASE_URL;}, 3500);                    
                }     
            }  
        }); // end AJAX upita 
        
       
    });
    
    
    $("#login_submit").click(
      function(){
        var loginData = $("#form-login").serialize();
        var ajaxURL = AJAX_URL + 'login/';
    
        // Ajax upit
        $.ajax({  
            type: "POST", 
            url: ajaxURL,
            data: loginData,
            cache: false,
            async: false,
            success: function(response){

                var parsed = JSON.parse(response);
                var html = '';

                if(parseInt(parsed.ind) == 1)
                {
                    // Nakon logovanja ide na stranicu profila
                    window.location = parsed.redirect_url;
                }
                else
                {      
                    html += "<p>"+parsed.msg +"</p>";
                    $('#response_login').show().html(html).delay(2500).hide(500); 
                }            
            }  
        }); // end AJAX upita 
    });
    
});
